package com.example.demo.controller;

import com.example.demo.entity.Player;
import com.example.demo.service.PlayerService;
import com.example.demo.service.impl.PlayerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/players")
@Api(tags = "球员管理") // 控制器标签
public class PlayerController {

    @Autowired
    private PlayerService playerServiceImpl;

    // 获取所有球员
    @GetMapping("/getPlayersList")
    @ApiOperation("获取所有球员")
    public List<Player> getAllPlayers() {
        return playerServiceImpl.getAllPlayers();
    }

    // 根据ID获取球员
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取球员")
    public Player getPlayerById(@PathVariable Long id) {
        return playerServiceImpl.getPlayerById(id).orElse(null);
    }

    // 新增或更新球员
    @PostMapping("/addPlayers")
    @ApiOperation("新增或更新球员")
    public Player createOrUpdatePlayer(@RequestBody Player player) {
        return playerServiceImpl.savePlayer(player);
    }

    // 删除球员
    @DeleteMapping("/{id}")
    @ApiOperation("删除球员")
    public void deletePlayer(@PathVariable Long id) {
        playerServiceImpl.deletePlayerById(id);
    }
}
